package com.demo.starter.service.impl;

import com.demo.starter.domain.vo.ArticleDetailVO;
import com.demo.starter.entity.Article;
import com.demo.starter.entity.ArticleBody;
import com.demo.starter.entity.ArticleBodyExample;
import com.demo.starter.entity.ArticleExample;
import com.demo.starter.repository.ArticleBodyMapper;
import com.demo.starter.repository.ArticleMapper;
import com.demo.starter.service.IArticleService;
import com.demo.starter.status.ArticleStatusEnum;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleBodyMapper articleBodyMapper;

    @Override
    public List<ArticleDetailVO> listHot(Integer pageNo, Integer pageSize) {
        List<ArticleDetailVO> result = new ArrayList<>();
        RowBounds rowBounds = new RowBounds((pageNo - 1) * pageSize, pageSize);
        final ArticleExample example = new ArticleExample();
        example.createCriteria()
                .andStatusEqualTo(ArticleStatusEnum.SHOW.getStatus())
                .andIsHotEqualTo(true);
        final List<Article> list = articleMapper.selectByExampleWithRowbounds(example, rowBounds);
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        for (Article a : list) {
            ArticleDetailVO vo = wrapArticleDetail(a);
            result.add(vo);
        }
        return result;
    }

    @Override
    public List<ArticleDetailVO> listByCategoryId(Long categoryId) {
        List<ArticleDetailVO> result = new ArrayList<>();
        ArticleExample example = new ArticleExample();
        example.setOrderByClause("id DESC");
        example.createCriteria()
                .andCategoryIdEqualTo(categoryId)
                .andStatusEqualTo(ArticleStatusEnum.SHOW.getStatus());
        final List<Article> list = articleMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return result;
        }
        for (Article a : list) {
            ArticleDetailVO vo = wrapArticleDetail(a);
            result.add(vo);
        }
        return result;
    }

    @Override
    public ArticleDetailVO getById(Long id) {
        final Article article = articleMapper.selectByPrimaryKey(id);
        if (article != null) {
            return wrapArticleDetail(article);
        }
        return null;
    }

    /**
     * 查询 article 正文
     *
     * @param articleId
     * @return
     */
    private String queryContentByArticleId(Long articleId) {
        ArticleBodyExample example = new ArticleBodyExample();
        example.createCriteria().andArticleIdEqualTo(articleId);
        final List<ArticleBody> list = articleBodyMapper.selectByExampleWithBLOBs(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0).getContent();
    }

    /*
     * 封装 ArticleDetailVO
     * @param a
     * @return
     */
    private ArticleDetailVO wrapArticleDetail(Article a) {
        ArticleDetailVO vo = new ArticleDetailVO(a);
        final Long articleId = a.getId();
        vo.setId(articleId);
        vo.setTitle(a.getTitle());
        vo.setSummary(a.getDescription());
        vo.setBody(queryContentByArticleId(articleId));
        return vo;
    }

}
